package com.beidouapp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beidouapp.common.util.DateUtils;
import com.beidouapp.common.util.UUIDGenerator;
import com.beidouapp.domain.pojo.Engineer;
import com.beidouapp.domain.pojo.PrjProgress;
import com.beidouapp.domain.pojo.PrjProgressSummary;
import com.beidouapp.domain.pojo.Project;
import com.beidouapp.domain.vo.ProjectVo;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.manager.EngineerManager;
import com.beidouapp.manager.PrjProgressManager;
import com.beidouapp.manager.ProjectManager;
import com.beidouapp.service.ProjectService;
import com.beidouapp.translator.TranslatorManager;
@Service
public class ProjectServiceImpl implements ProjectService{
	@Autowired
	ProjectManager projectManager;
	
	@Autowired
	PrjProgressManager prjProgressManager;
	
	@Autowired
	TranslatorManager translatorManager;
	
	@Autowired
	EngineerManager engineerManager;

	@Override
	public List<Project> findProjectByMap(Map<String, Object> params,
			Integer offset, Integer limit) {
		
		List<Project> list=projectManager.findProjectByMap(params, offset, limit);
		return translatorManager.translate(list);
	}

	@Override
	public Integer findProjectCountByMap(Map<String, Object> params) {
		 
		return projectManager.findProjectCountByMap(params);
	}

	@Override
	public ProjectVo getProjectVoById(String id) {
		 if(StringUtils.isBlank(id)){
			 throw new BusinessException("项目对象不存在！");
		 }
		 Project project=projectManager.getProjectById(id);
		 if(null==project){
			 throw new BusinessException("当前项目不存在！");
		 }
		 if(StringUtils.isBlank(project.getName())){
			 project.setName(project.getUsername());//真实姓名为空时 使用用户名
		 }
		 String pgCode=project.getProgressTypeCode();
		 ProjectVo projectVo=new ProjectVo();
		 BeanUtils.copyProperties(project, projectVo);
		 Map<String, Object> map=new HashMap<String, Object>();
		 map.put("projectId",id);
		 List<PrjProgress>	progresses= projectManager.findPrjProgressByMap(map);
		 progresses=progresses==null?new ArrayList<PrjProgress>():progresses;
		 projectVo.setProgresses(progresses);
		 if(progresses.size()!=0 && StringUtils.isNotBlank(project.getProgressTypeCode())){
			 String pcode="";
			 for(PrjProgress prjProgress:progresses){
				 pcode=prjProgress.getProgressTypeCode();
				 pcode=pcode==null?"":pcode;
				 Date followDate=prjProgress.getFollowTime();
				 if(null!=followDate){
					 prjProgress.setFollowTimeStr(DateUtils.format(followDate, "yyyy-MM-dd"));
				 }
				 if(StringUtils.isBlank(prjProgress.getEnteringManName())){
					 prjProgress.setEnteringManName(prjProgress.getEnteringUserName());
				 }
				 if(StringUtils.isBlank(prjProgress.getOnlineFollowManName())){
					 prjProgress.setOnlineFollowManName(prjProgress.getOnlineUserName());
				 }
				 if(StringUtils.isBlank(prjProgress.getOfflineFollowManName())){
					 prjProgress.setOfflineFollowManName(prjProgress.getOfflineUserName());
				 }
				 
				 if(pcode.equals(pgCode)){
					 projectVo.setOnlineMan(prjProgress.getOnlineFollowManName());
					 projectVo.setOfflineMan(prjProgress.getOfflineFollowManName());
				 }
				 
			 }
		 }
		 
		 
		 return projectVo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PrjProgress> findPrjProgressByMap(String projId) {
		Map params=new HashMap();
		params.put("projectId",projId);
		return projectManager.findPrjProgressByMap(params);
	}

	@SuppressWarnings("unused")
	@Override
	public Integer saveOrgUpdateProject(Project project) {
		
		if(null==project){
			throw new BusinessException("更新对象不能为null");	
		}
		String userName=project.getName();
		if(StringUtils.isBlank(userName)){
			throw new BusinessException("当前用户不存在！");
		}
		Integer count=engineerManager.getEngineersCountByName(userName);
		if(null==count || 0==count){
			throw new BusinessException("当前选择的用户不存在！");
		}
		Integer id=project.getId();
		if(null==id){
			projectManager.save(project);
			id=project.getId();
		}else{
			projectManager.update(project);
		}
		return id;
	}

	@Override
	public String saveOrgUpdatePrjProgress(PrjProgress prjProgress) {
		if(null==prjProgress){
			throw new BusinessException("编辑对象不能为null");	
		}
		String pId=prjProgress.getId();
		if(StringUtils.isBlank(pId)){
			pId=UUIDGenerator.get32UUID();
			prjProgress.setId(pId);
			projectManager.savePrjProgress(prjProgress);
		}else{
			projectManager.updatePrjProgress(prjProgress);
		}
		if(StringUtils.isNotBlank(prjProgress.getProjectId()) && !"null".equals(prjProgress.getProjectId())){
			Project project=new Project();
			project.setId(Integer.valueOf(prjProgress.getProjectId()));
				PrjProgress	progress=projectManager.getMaxFollowTimePrj(project.getId()+"");
				if(null!=progress && StringUtils.isNotBlank(progress.getProgressTypeCode())){
					project.setProgressTypeCode(progress.getProgressTypeCode());
					projectManager.update(project);
				}
		}
		return pId;
		
	}

	@Override
	public List<PrjProgressSummary> listPrjProgressSummary() {
		return projectManager.listPrjProgressSummary();
	}

	@Override
	public Project getOnAndOffLineMan(Project project) {
		return projectManager.getOnAndOffLineMan(project);
	}

	@Override
	public PrjProgress getPrjProgressById(String id) {
		// TODO Auto-generated method stub
		return projectManager.getPrjProgressById(id);
	}
}
